#! /usr/bin/env python
#coding:cp936
#BUG FILTER

def powersplit(l, key):
    s = list(set([a[key] for a in l]))
    ret = {}
    for i in s:
        ret[i] = []
    for i in l:
        ret[i[key]].append(i)
    return ret

def poweralter(l, key, d):
    for i in l:
        i[key] = d[i[key]]
    return l

def actioncache(db,l, value):
    sqlstr = """    select \
        journalized_id issue_id,\
        journals.created_on,\
        old_value,\
        value \
        from journals \
        left join journal_details on journal_details.journal_id = journals.id \
        where journal_details.prop_key = 'status_id' and value in """+repr(tuple(map(str, value)))+""" and \
        journalized_id in """ + repr(tuple(l)) + """ order by issue_id"""
    cache = db.query(sqlstr)
    return cache

def getclosetime(l, db, done_status_ids):#type(done_status_ids) == 'tuple'
    idlist = [int(a['id']) for a in l]
    idlist.extend([-1,-2])
    cache = actioncache(db, idlist, done_status_ids)
    cache_dict = powersplit(cache, 'issue_id')
    cmpf = lambda a,b : cmp(a['created_on'], b['created_on'])
    for item in l:
        if item['status_id'] in done_status_ids:
            if item['id'] in cache_dict.keys():
                tmp = sorted(cache_dict[item['id']], cmp = cmpf)[0]['created_on']
            else:#impossible
                tmp = item['ctn']
            item['closetime'] = tmp
            item['close'] = True
        else:
            item['closetime'] = None
            item['close'] = False
        if item['closetime']:
            #item['month'] = str(item['closetime'].year) + str(item['closetime'].month)
            #item['month'] = str(item['closetime'].year) + "%x"%(item['closetime'].month)
            #item['month'] = item['closetime'].year*12 + item['closetime'].month
            item['month'] = item['ctn'].year*12 + item['ctn'].month
        else:
            #item['month'] = str(item['ctn'].year) + str(item['ctn'].month)
            item['month'] = item['ctn'].year*12 + item['ctn'].month
    return l

def x5_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 20,project_id = 7,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def xy2_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 5,project_id = 4,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def xy3_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 5,project_id = 4,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def xyq_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 5,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def xyw_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 20,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def csxy_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    #ids = [int(a['customized_id']) for a in db.custom_values.where(custom_field_id=3, value__neq='', customized_type='Issue').select() ]
    ret = db.issues.where( tracker_id = 5,project_id = 4,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def tx2_bug_filter(db,user_list,userdict):
    DONE_STATUS = [6,-1]
    ret = db.issues.where( tracker_id = 6,project_id = 6,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def itown_bug_filter(db,user_list,userdict):
    return

def ff_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 5,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def dtw_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 5,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def dtws_bug_filter(db,user_list,userdict):
    return

def mc_bug_filter(db,user_list,userdict):
    DONE_STATUS = [15,-1]
    ret = db.issues.where( tracker_id = 1,project_id = 2,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def ysg_bug_filter(db,user_list,userdict):
    return

def gsz_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 5,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def neox_bug_filter(db,user_list,userdict):
    DONE_STATUS = [5,-1]
    ret = db.issues.where( tracker_id = 1,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def x2_bug_filter(db,user_list,userdict):
    DONE_STATUS = [5,-1]
    ret = db.issues.where( tracker_id = 4,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def px3_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 5,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def cc_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,-1]
    ret = db.issues.where( tracker_id = 18,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key

def f_bug_filter(db,user_list,userdict):
    DONE_STATUS = [3,6,-1]
    ret = db.issues.where( tracker_id = 5,author_id__in = user_list ).select('id,status_id,author_id,created_on ctn')
    ret = getclosetime(ret, db, DONE_STATUS)
    ret = poweralter(ret, 'author_id', userdict)
    return powersplit(ret, 'author_id')#return a dict,user id is key
